package com.imooc.forkcgz;

import android.app.ActivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView fibonacci_list;
    private ListViewAdapter mListViewAdapter;
    private LinkedHashMap<Integer, String> intBeen;
    private ArrayList<FibonAsyncTask> fibonAsyncTaskArrayList;

    private Button button;
    private boolean isUp = true;
    private final static int GC_SIZE = 48 * 1024 * 1024;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityManager systemService = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
        systemService.getLargeMemoryClass();
        initView();

        initAdapter();

        initData();
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            mListViewAdapter.setType(450);
            return false;
        }
    });


    /**
     * 初始化控件
     */
    private void initView() {
        fibonacci_list = (ListView) findViewById(R.id.fibonacci_list);
        button = (Button) findViewById(R.id.button);
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        intBeen = new LinkedHashMap<>();
        fibonAsyncTaskArrayList = new ArrayList<>();
        mListViewAdapter = new ListViewAdapter(this, fibonAsyncTaskArrayList, intBeen);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        finish();
//        System.exit(0);
    }

    /**
     * 绑定数据
     */
    private void initData() {
        fibonacci_list.setAdapter(mListViewAdapter);
        mHandler.sendEmptyMessageDelayed(0, 1000);
        button.setOnClickListener(this);
        fibonacci_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    // 停止滑动
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
//                        mListViewAdapter.setScrollState(false);
                        break;
                    // 惯性滑动
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
//                        mListViewAdapter.setScrollState(true);
                        clear();

                        break;
                    // 正在滑动
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
//                        mListViewAdapter.setScrollState(true);
                        clear();

                        break;
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        clear();


    }

    private void clear() {
        // 清空 异步任务队列
        if (fibonAsyncTaskArrayList.size() > 0) {
            for (FibonAsyncTask fibonAsyncTask : fibonAsyncTaskArrayList) {
                if (fibonAsyncTask != null && fibonAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
                    fibonAsyncTask.cancel(true);
                    Log.e("MainActivity", "不为空");
                } else {
                    Log.e("MainActivity", "为空");
                }
            }
        }
        fibonAsyncTaskArrayList.clear();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        if (isUp) {
            button.setText("降序");
            isUp = false;
            clear();
        } else {
            button.setText("升序");
            isUp = true;
            clear();
        }
        mListViewAdapter.setUp(isUp);

    }
}
